/*
 * @author ${user}
 * @date ${year}-${month}-${day}
 */

package net.xdclass.config;

import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

@Configuration
@Data
public class RabbitMQConfig {

	private String orderCloseDelayQueue = "order.close.delay.queue";
	private String orderCloseQueue = "order.close.queue";

	private String orderEventExchange = "order.event.exchange";

	private String orderCloseDelayRoutingKey = "order.close.delay.routing.key";
	private String orderCloseRoutingKey = "order.close.routing.key";

	private Integer ttl = 1000 * 60;

	@Bean
	public Queue OrderCloseDelayQueue(){
		Map<String,Object> args = new HashMap(3);
		args.put("x-dead-letter-exchange",orderEventExchange);
		args.put("x-dead-letter-routing-key",orderCloseRoutingKey);
		args.put("x-message-ttl",ttl);
       return  new Queue(orderCloseDelayQueue,true,false,false,args);
	}

	@Bean
	public Queue OrderCloseQueue(){
		return  new Queue(orderCloseQueue,true,false,false);
	}

	@Bean
	public Exchange OrderEventExchange(){
		return new TopicExchange(orderEventExchange,true,false);
	}

    @Bean
	public Binding OrderCloseDelayBinding(){
		return new Binding(orderCloseDelayQueue, Binding.DestinationType.QUEUE,
				orderEventExchange,orderCloseDelayRoutingKey,null);
	}

	@Bean
	public Binding OrderCloseBinding(){
		return new Binding(orderCloseQueue, Binding.DestinationType.QUEUE,
				orderEventExchange,orderCloseRoutingKey,null);
	}

	// 自定义消息转化器
	// 作为 RabbitMQ 默认的消息序列化/反序列化工具
	@Bean
	public MessageConverter messageConverter(){
		return new Jackson2JsonMessageConverter();
	}


}
